import { Controller, Get, Post, Body, Query,UseGuards,Headers  } from '@nestjs/common';
import { UserService } from './user.service';
import {registerDto,searchUserByName } from './type';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from '../auth/auth.service'
import { ApiTags, ApiOperation, ApiQuery, ApiParam ,ApiBody} from '@nestjs/swagger';
import { IpAddress } from '../../decorator/getip.decorator'

@Controller('user')
@ApiTags('user')
export class UserController {
  constructor(
    private readonly authService: AuthService, 
    private readonly userService: UserService
  ) {}

  @Post('register')
  @ApiOperation({ summary: '注册' })
  @ApiBody({type: registerDto})
  register(@Body() params: registerDto,@IpAddress() clinetIp:string): any {
    return this.userService.register({...params,ip:clinetIp});
  }

  @Post('login')
  @ApiOperation({ summary: '登录' })
  async login(@Body() params: registerDto): Promise<any> {
    const authResult = await this.authService.validateUser(params.username, params.password);
    const token = await this.authService.certificate(authResult);
    const userinfo = await this.userService.findOneByusername(params.username);
    console.log(userinfo);
    return {
      token: token,
      userinfo: {
        name: userinfo.username,
        sex: userinfo.sex,
        createTime: userinfo.createTime,
        id: userinfo.id
      }
    };
  }
  
  @UseGuards(AuthGuard('jwt'))
  @Get('findOne')
  @ApiOperation({ summary: '用户名模糊查询' })
  findOne(@Query() query: searchUserByName): any {
      return this.userService.findOne(query);
  }
}
